import pygame, plane_bullet

# 定义一个常量(赋值后不能修改)常量一般使用大写字母
IMAGE_PATH = "D:\\MyLinux\\Ubuntu\\Sharedfiles\\6temp\\python-learning\\Airplanewar\\res\\"

# 自定义一个英雄飞机类
class HeroPlane(object):

    def __init__(self, mapWidth = 512, mapHeight = 768):
        self.mapWidth = mapWidth
        self.mapHeight = mapHeight

        self.img = pygame.image.load(IMAGE_PATH + "hero2.png") # 赋值
        self.img_rect = self.img.get_rect() # 获取图片矩形
        self.img_rect.move_ip((self.mapWidth - self.img_rect[2])/2, self.mapHeight - self.img_rect[3] - 50) # 设置飞机的初始位置
        self.speed = 1     # 飞机速度
        self.bullet_list = [plane_bullet.PlaneBullet() for i in range(6)] # 子弹弹夹

    # 向上移动
    def move_up(self):
        if self.img_rect[1] >= 0: # 边缘检测
            self.img_rect.move_ip(0, -self.speed)

    # 向下移动
    def move_down(self):
        if self.img_rect[1] <= self.mapHeight - self.img_rect[3]: # 边缘检测
            self.img_rect.move_ip(0, self.speed)

    # 向左移动
    def move_left(self):
        if self.img_rect[0] >= 0: # 边缘检测
            self.img_rect.move_ip(-self.speed, 0)

    # 向右移动
    def move_right(self):
        if self.img_rect[0] <= self.mapWidth - self.img_rect[2]: # 边缘检测
            self.img_rect.move_ip(self.speed, 0)

    # 发射子弹
    def shot(self):
        for bullet in self.bullet_list:# 遍历所有的子弹
            if not bullet.is_shot:     # 判断未发射的
                # 设置子弹的位置
                bullet.img_rect[0] = self.img_rect[0] + (self.img_rect[2] - bullet.img_rect[2])/2
                bullet.img_rect[1] = self.img_rect[1] - bullet.img_rect[3]
                bullet.is_shot = True # 设置为发射状态
                break # 一次只能发射一颗子弹